package com.order.controller.seller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.order.pojo.OrderMaster;
import com.order.service.IOrderDetailService;
import com.order.service.IOrderMasterService;
import com.order.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("statistics")
public class StatisticsController {

    @Autowired
    IOrderMasterService orderMasterService;

    @Autowired
    IOrderDetailService orderDetailService;

    @GetMapping("overview")
    public Result overview() {
        String startDate = DateUtil.now().split(" ")[0] + " 00:00:00";
        String endDate = DateUtil.now().split(" ")[0] + " 23:59:59";
        int todayOrders = orderMasterService.count(new QueryWrapper<OrderMaster>().between("create_time", startDate, endDate));

        int activeUsers = orderMasterService.count(new QueryWrapper<OrderMaster>().groupBy("buyer_openid"));
        int totalProducts = orderDetailService.count(new QueryWrapper<>());
        BigDecimal totalAmount = orderMasterService.sumMoney();
        Map<String, Object> map = new HashMap<>();
        map.put("todayOrders", todayOrders);
        map.put("activeUsers", activeUsers);
        map.put("totalProducts", totalProducts);
        map.put("totalAmount", totalAmount);
        return Result.success(map);
    }
}
